package smecta.client.print.controller;

import java.util.ArrayList;
import java.util.List;

import smecta.client.print.util.DatabaseUtil;
import smecta.client.print.util.RowData;
import smecta.client.print.util.TableUtil;

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class TitleSettingsController {

	@FXML
	private TextField Title1, Title2, Title3, Title4, Title5, Title6, Title7, Title8, Title9, Title10;
	@FXML
	private TextField Title11, Title12, Title13, Title14, Title15, Title16, Title17;
	@FXML
	private TextField width1, width2, width3, width4, width5, width6, width7, width8, width9, width10;
	@FXML
	private TextField width11, width12, width13, width14, width15, width16, width17;
	@FXML
	private CheckBox unitCheckBox;
	@FXML
	private Button saveButton, closeButton;

	private TableView<RowData> dataTableView;

	List<TextField> TitleList=null;
	List<TextField> widthList=null;

	@FXML
	public void initialize() {
		TitleList=new ArrayList<TextField>();
		widthList=new ArrayList<TextField>();
		System.out.println("Title1:"+Title1);
		TitleList.add(Title1);
		TitleList.add(Title2);
		TitleList.add(Title3);
		TitleList.add(Title4);
		TitleList.add(Title5);
		TitleList.add(Title6);
		TitleList.add(Title7);
		TitleList.add(Title8);
		TitleList.add(Title9);
		TitleList.add(Title10);
		TitleList.add(Title11);
		TitleList.add(Title12);
		TitleList.add(Title13);
		TitleList.add(Title14);
		TitleList.add(Title15);
		TitleList.add(Title16);
		TitleList.add(Title17);
		widthList.add(width1);
		widthList.add(width2);
		widthList.add(width3);
		widthList.add(width4);
		widthList.add(width5);
		widthList.add(width6);
		widthList.add(width7);
		widthList.add(width8);
		widthList.add(width9);
		widthList.add(width10);
		widthList.add(width11);
		widthList.add(width12);
		widthList.add(width13);
		widthList.add(width14);
		widthList.add(width15);
		widthList.add(width16);
		widthList.add(width17);

	}
	public void setDataTableView(TableView<RowData> dataTableView) {
		this.dataTableView = dataTableView;
	}

	public void readNameAndWithFromTable() {
		for(TableColumn col:dataTableView.getColumns())
		{
			System.out.println(col.getId()+":"+col.getText());
			int index= getTextFieldById(TitleList,col.getId());
			if(index<0)continue;
			TitleList.get(index).setText(col.getText());
			widthList.get(index).setText(""+col.getWidth());
		}
	}
	public int getTextFieldById(List<TextField> list,String id) {
		for(int i=0;i<list.size();i++)
		{
			if(list.get(i).getId().equalsIgnoreCase(id))
			{
				return i;
			}
		}
		return -1;
	}

	@FXML
	private void handleSaveSettings() {
		// 实现保存设置的逻辑
		// 可以将所有 TextField 中的值保存到配置文件或数据库中
		System.out.println("保存设置");
		for(int i=0;i<TitleList.size();i++)
		{
			TextField nameTxt=TitleList.get(i);
			TextField widthTxt=widthList.get(i);
			for(TableColumn col:dataTableView.getColumns())
			{
				if(col.getId().equalsIgnoreCase(nameTxt.getId()))
				{
					col.setText(nameTxt.getText());
					col.setPrefWidth(Double.parseDouble(widthTxt.getText()));
					break;
				}
			}
		}
	}

	@FXML
	private void handleClose() {
		// 关闭窗口
		Stage stage = (Stage) closeButton.getScene().getWindow();
		stage.close();
	}
}


